package com.plum.admin.modules.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.plum.admin.common.constant.Constant;
import com.plum.admin.common.util.MenuUtil;
import com.plum.admin.modules.sys.entity.SysMenu;
import com.plum.admin.modules.sys.mapper.SysMenuMapper;
import com.plum.admin.modules.sys.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plum.admin.modules.sys.vo.MenuTreeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Override
    public List<String> findPermissionByUserId(String userId) {
        return baseMapper.findPermissionByUserId(userId);
    }

    @Override
    public List<String> findMenuByUserId(String userId) {
        return baseMapper.findMenuByUserId(userId);
    }

    @Override
    public List<MenuTreeVo> queryMenuList(Integer isLoadButton) {
        List<SysMenu> menuList = baseMapper.selectList(Wrappers.emptyWrapper());
        List<MenuTreeVo> treeVoList = menuList.stream()
                .filter(menu -> menu.getParentId().equals("0") && StrUtil.isNotBlank(menu.getParentId()))
                .map(m -> {
                    MenuTreeVo menuTreeVo = new MenuTreeVo();
                    BeanUtils.copyProperties(m, menuTreeVo);
                    menuTreeVo.setMenuId(m.getId());
                    menuTreeVo.setLabel(m.getTitle());
                    menuTreeVo.setParentTitle("一级菜单");
                    menuTreeVo.setValue(m.getId());
                    menuTreeVo.setKey(m.getId());
                    return menuTreeVo;
                }).collect(Collectors.toList());
        MenuUtil.findMenuChildren(treeVoList,menuList,isLoadButton);
        return treeVoList;
    }

    @Override
    public JSONArray queryRouter(String userId) {
        LambdaQueryWrapper<SysMenu> menuWrapper = Wrappers.<SysMenu>query().lambda();
//        if(!userId.equals(Constant.ADMIN_USER_ID)){
//            List<String> list = this.findMenuByUserId(userId);
//            menuWrapper.in(SysMenu::getId,list);
//        }
        List<String> list = this.findMenuByUserId(userId);
        menuWrapper.in(SysMenu::getId,list);
        menuWrapper.orderByAsc(SysMenu::getSort);
        List<SysMenu> menuList = baseMapper.selectList(menuWrapper);
        JSONArray jsonArray=new JSONArray();
        MenuUtil.getRouterArray(jsonArray,menuList,null);
        return jsonArray;
    }

    @Override
    public List<MenuTreeVo> querySelectMenu(Integer isLoadButton) {
        List<SysMenu> menuList = baseMapper.selectList(Wrappers.emptyWrapper());
        List<MenuTreeVo> treeVoList = menuList.stream()
                .filter(menu -> menu.getParentId().equals("0") && StrUtil.isNotBlank(menu.getParentId()))
                .map(m -> {
                    MenuTreeVo menuTreeVo = new MenuTreeVo();
                    BeanUtils.copyProperties(m, menuTreeVo);
                    menuTreeVo.setMenuId(m.getId());
                    menuTreeVo.setLabel(m.getTitle());
                    menuTreeVo.setParentTitle("一级菜单");
                    menuTreeVo.setValue(m.getId());
                    menuTreeVo.setKey(m.getId());
                    return menuTreeVo;
                }).collect(Collectors.toList());
        MenuTreeVo menuTreeVo=new MenuTreeVo();
        menuTreeVo.setMenuId("-1");
        menuTreeVo.setLabel("一级菜单");
        menuTreeVo.setKey("-1");
        menuTreeVo.setSort(0);
        menuTreeVo.setParentId("0");
        treeVoList.add(menuTreeVo);
        treeVoList= treeVoList.stream()
                .sorted(Comparator.comparing(MenuTreeVo::getSort))
                .collect(Collectors.toList());
        MenuUtil.findMenuChildren(treeVoList,menuList,isLoadButton);
        return Lists.newArrayList(treeVoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(List<String> idList) {
        baseMapper.deleteBatchIds(idList);
        baseMapper.deleteByMenuId(idList);
    }
}
